{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 测试LED灯代码，按SHIFT+ENTER，８个小灯一次点亮，俗称流水灯\n",
    "##注：本段代码，复制自：https://github.com/spoonysonny/SAKS-tutorials/tree/master/water-lights"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "程序终止\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "#\n",
    "# Copyright (c) 2016 NXEZ.COM.\n",
    "# http://www.nxez.com\n",
    "#\n",
    "# Licensed under the GNU General Public License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#   http://www.gnu.org/licenses/gpl-2.0.html\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "# tutorials url: http://shumeipai.nxez.com/2015/03/23/saks-diy-tutorials-water-lights.html\n",
    "\n",
    "__author__ = 'Spoony'\n",
    "__license__  = 'Copyright (c) 2016 NXEZ.COM'\n",
    " \n",
    "import RPi.GPIO as GPIO\n",
    "import time\n",
    "\n",
    "GPIO.setmode(GPIO.BCM)\n",
    "\n",
    "DS = 6\n",
    "SHCP = 19\n",
    "STCP = 13\n",
    "\n",
    "def init():\n",
    "    GPIO.setup(DS, GPIO.OUT)\n",
    "    GPIO.setup(SHCP, GPIO.OUT)\n",
    "    GPIO.setup(STCP, GPIO.OUT)\n",
    "\n",
    "    GPIO.output(DS, GPIO.LOW)\n",
    "    GPIO.output(SHCP, GPIO.LOW)\n",
    "    GPIO.output(STCP, GPIO.LOW)\n",
    "\n",
    "def writeBit(data):\n",
    "    GPIO.output(DS, data)\n",
    "\n",
    "    GPIO.output(SHCP, GPIO.LOW)\n",
    "    GPIO.output(SHCP, GPIO.HIGH)\n",
    "\n",
    "#写入8位LED的状态\n",
    "def writeByte(data):\n",
    "    for i in range (0, 8):\n",
    "        writeBit((data >> i) & 0x01)\n",
    "    #状态刷新信号\n",
    "    GPIO.output(STCP, GPIO.LOW)\n",
    "    GPIO.output(STCP, GPIO.HIGH)\n",
    "\n",
    "try:\n",
    "    init()\n",
    "    while True:\n",
    "        #以下一组8个编码由一组二进制转换而成：\n",
    "        #00000001,00000010,00000100,00001000,00010000,00100000,01000000,10000000\n",
    "        #分别对应8个LED点亮状态\n",
    "        for i in [0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80]:\n",
    "            writeByte(i)\n",
    "            time.sleep(0.1)\n",
    "        #LED组全开\n",
    "        #writeByte(0xff)\n",
    "        #time.sleep(0.1)\n",
    "\n",
    "except KeyboardInterrupt:\n",
    "    print (\"程序终止\")\n",
    "    #LED组全关\n",
    "    writeByte(0x00)\n",
    "    #GPIO.cleanup()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
